﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.UnityExtensions;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Presentation.Regions;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Events;

using Microsoft.Practices.Composite.Logging;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.ServiceLocation;



namespace Skynet.MeshClient
{
    public class Bootstrapper : UnityBootstrapper
    {
        protected override Microsoft.Practices.Unity.IUnityContainer CreateContainer()
        {


            var Container = Infrastructure.Aspect.Patterns.Singleton<UnityContainer, IUnityContainer>.Instance;

            var fileMapPolicies = new ExeConfigurationFileMap { ExeConfigFilename = @".\Config\Aspect.config" };
            var configurationPolicies = ConfigurationManager.OpenMappedExeConfiguration(fileMapPolicies, ConfigurationUserLevel.None);
            var unitySectionPolicies = (UnityConfigurationSection)configurationPolicies.GetSection("unity");
            Container.LoadConfiguration(unitySectionPolicies);


            var fileMapDataAccess = new ExeConfigurationFileMap { ExeConfigFilename = @".\Config\DataAccess.config" };
            var configurationDataAccess = ConfigurationManager.OpenMappedExeConfiguration(fileMapDataAccess, ConfigurationUserLevel.None);
            var unitySectionDataAccess = (UnityConfigurationSection)configurationDataAccess.GetSection("unity");
            Container.LoadConfiguration(unitySectionDataAccess);

            var fileMapBusiness = new ExeConfigurationFileMap { ExeConfigFilename = @".\Config\Business.config" };
            var configurationBusiness = ConfigurationManager.OpenMappedExeConfiguration(fileMapBusiness, ConfigurationUserLevel.None);
            var unitySectionBusiness = (UnityConfigurationSection)configurationBusiness.GetSection("unity");
            Container.LoadConfiguration(unitySectionBusiness);

            return Container;
        }
        protected override System.Windows.DependencyObject CreateShell()
        {
            var shell = this.Container.Resolve<MainWindow>();
            shell.Show();
            return shell;

        }
        protected override Microsoft.Practices.Composite.Presentation.Regions.RegionAdapterMappings ConfigureRegionAdapterMappings()
        {

            RegionAdapterMappings regionAdapterMappings = Container.TryResolve<RegionAdapterMappings>();
            //regionAdapterMappings.RegisterMapping(typeof(Selector), this.Container.Resolve<SelectorRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(ItemsControl), this.Container.Resolve<ItemsControlRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(Frame), this.Container.Resolve<Infrastructure.WpfClient.Composite.Regions.FrameRegion.FrameRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(Infrastructure.WpfClient.Composite.Regions.SingleActiveRegion.SingleActiveControl), this.Container.Resolve<Infrastructure.WpfClient.Composite.Regions.SingleActiveRegion.SingleActiveViewRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(Infrastructure.WpfClient.Composite.Regions.TabRegion.AnimatedTabControl), this.Container.Resolve<Infrastructure.WpfClient.Composite.Regions.TabRegion.TabRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(Infrastructure.WpfClient.Composite.Regions.LateralRegion.LateralRegionControl), this.Container.Resolve<Infrastructure.WpfClient.Composite.Regions.LateralRegion.LateralRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(Infrastructure.WpfClient.Composite.Regions.ModalDialog.DummyModalDialogContent), this.Container.Resolve<Infrastructure.WpfClient.Composite.Regions.ModalDialog.ModalDialogRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(Infrastructure.WpfClient.Composite.Regions.ModalWindow.DummyModalWindowContent), this.Container.Resolve<Infrastructure.WpfClient.Composite.Regions.ModalWindow.ModalWindowRegionAdapter>());
            return regionAdapterMappings;
        }
        protected override void ConfigureContainer()
        {
            Container.RegisterInstance<ILoggerFacade>(LoggerFacade);
            IModuleCatalog catalog = GetModuleCatalog();
            if (catalog != null)
            {
                this.Container.RegisterInstance(catalog);
            }

            this.Container.RegisterType<IServiceLocator, UnityServiceLocatorAdapter>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IModuleInitializer, ModuleInitializer>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IModuleManager, ModuleManager>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<RegionAdapterMappings>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IRegionManager, RegionManager>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IEventAggregator, EventAggregator>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IRegionViewRegistry, RegionViewRegistry>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IRegionBehaviorFactory, RegionBehaviorFactory>(new ContainerControlledLifetimeManager());
            ServiceLocator.SetLocatorProvider(() => this.Container.Resolve<IServiceLocator>());
            this.Container.RegisterType<Infrastructure.WpfClient.Services.Navigation.INavigationService, Infrastructure.WpfClient.Services.Navigation.NavigationService>(new ContainerControlledLifetimeManager());

        }
        protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
        {
            ModuleCatalog catalog = new ConfigurationModuleCatalog();
            return catalog;
        }
    }
}
